<?php

class Core_Component_System_Controller_Backend_Menu {
    
    public function __construct() {
        $GLOBALS[RC]->getHelper()->setLocation('{system.system}', '{system.backend_menu}');
    }
    
    public function mappingAction(){
        return array(
            'create'=>'edit'
        );
    }    
    
    public function indexAction($pageindex = 1, $pagesize = 20){
        $q = App::getModel('system/menu')->getQuery();
        $list = $q->orderby('a.parent_id', 'asc')->orderby('a.sort', 'asc')->pagination($pageindex, $pagesize)->fetch();
        $count = $q->fetchCount();
        return $GLOBALS[RC]->getHelper()->templateResult(array('list' => $list, 'record_count' => $count));        
    }   
    
    public function editAction($id = NULL) {
        $model = App::getModel('system/menu');
        $roles = App::getModel('account/role')->getQuery()->fetch()->toArray();
        $components = App::getModel('system/component')
                        ->getQuery()
                        ->fetch()
                        ->toArray();
        if (!is_null($id)) {
            $model->load($id);
            $menus = $model->getQuery()->condition('a.id', $id, 'neq')
                        ->fetch()
                        ->toArray();
            $GLOBALS[RC]->getHelper()->setLocation($model->getName());
        }else{
            $menus = $model->getQuery()
                           ->fetch()
                           ->toArray();
            $GLOBALS[RC]->getHelper()->setLocation('{system.backend_menu_create}');
        }
        return $GLOBALS[RC]->getHelper()->modelFormResult($model,
                array(
                    'role_codes'=>array('data'=>$roles, 'labelKey'=>'name', 'valueKey'=>'code'),
                    'parent_id'=>array('data'=>$menus, 'labelFormat'=>'<{lang "%s"}>'),
                    'component_id'=>array('data'=>$components, 'labelKey'=>'name', 'valueKey'=>'id', 'labelFormat'=>'<{lang "%s"}>')
                ),
                '*/*/edit');
    }

    public function saveAction(Core_Component_System_Model_Menu $model) {
        $model->merge()->validate()->save();
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->redirectResult('*/*/*/index');
    }    
    
    public function sortAction($parentid){
        $GLOBALS[RC]->getHelper()->setLocation('{system.backend_menu_sort}');
        $list = App::getModel('system/menu')
                    ->getQuery()
                    ->condition('parent_id', $parentid)
                    ->orderby('sort', 'asc')
                    ->fetch();
        return $GLOBALS[RC]->getHelper()->templateResult(array('list'=>$list->toArray()));
    }
    
    public function sortPostAction($ids){
        foreach($ids as $index=>$id){
            App::getModel('system/menu')
                    ->loadBy(array('a.id'=>$id))
                    ->setSort($index + 1)
                    ->save();
        }
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');         
    }    
}